<?php namespace Admin\Controller;
use Hdphp\Controller\Controller;
//文章管理控制器
class ArcController extends CommonController{
	private $model;
	public function __init(){
		//调用CommonController里面的构造函数
		parent::__init();
		//实例化arc模型
	  	$this->model = new \Common\Model\Arc;
	}
	
	//添加文章
	public function add(){
		//3.用户提交
		if(IS_POST){
			//文章添加
		   if(!$this->model->store()) View::error($this->model->getError());
		    View::success('添加成功',U('index'));
		}
		//1.处理“所属分类”
		$model = new \Common\Model\Cate;
		$cateData = Data::tree($model->get(),'cname');
//		p($cateData);exit;
		View::with('cateData',$cateData);
		//2.处理“文章标签”
		$model = new \Common\Model\Tag;
		$tagData = $model->get();
//		p($tagData);exit;	
		//因为用户可能多选标签，所以tid[]形式
		View::with('tagData',$tagData);
	    View::make();
	}
	
	//展示文章
	public function index(){
		//统计总条数
		$total =  $this->model->join('category','category_cid','=','cid')->where("is_recycle=0")->count();
		//分页类使用
		$page = Page::row(2)->make($total);
		View::with('page',$page);
		
		//SELECT * FROM article JOIN category ON category_cid=cid;
		$data = $this->model->join('category','category_cid','=','cid')->limit(Page::limit())->where("is_recycle=0")->get();
		
		//分配数据
		View::with('data',$data);
	    View::make();
	}
	
	//删除到回收站
	public function del(){
	    //就是把is_recycle改成1
	    $this->model->where("aid=" . Q('get.aid',0,'intval'))->save(array('is_recycle'=>1));
		View::success('删除成功',U('index'));
	}
	
	//回收站
	public function recycle(){
		//统计总条数
		$total =  $this->model->join('category','category_cid','=','cid')->where("is_recycle=1")->count();
		//分页类使用
		$page = Page::row(2)->make($total);
		View::with('page',$page);
		
		//SELECT * FROM article JOIN category ON category_cid=cid;
		$data = $this->model->join('category','category_cid','=','cid')->limit(Page::limit())->where("is_recycle=1")->get();
		
		//分配数据
		View::with('data',$data);
	    View::make();
	}
	
	//还原
	public function hy(){
		//把要还原的aid拿来和文章表里的aid比对，找到符合条件的把is_recycle改成0
		$this->model->where("aid=" . Q('get.aid',0,'intval'))->save(array('is_recycle'=>0));
		//成功提示
		View::success('还原成功',U('index'));
	}
	
	//真正的删除
	public function realDel(){
		//获得要删除数据的aid
		$aid = Q('get.aid',0,'intval');
	    //删除对应文章表
		$this->model->where("aid={$aid}")->delete();
		//删除对应的文章表
		$ArcDataModel = new \Common\Model\ArcData;
		$ArcDataModel->where("article_aid={$aid}")->delete();
		//删除对应的标签中间表
		$ArcTagModel = new \Common\Model\ArcTag;
		$ArcTagModel->where("article_aid={$aid}")->delete();
		//成功提示
		View::success('删除成功',U('recycle'));
	   
	}
	
	//编辑
	public function edit(){
		//5.修改
		if(IS_POST){
			if(!$this->model->edit()) View::error($this->model->getError());
			View::success('编辑成功',U('index'));
		}
		//1.获取“所属分类”
		$cateModel = new \Common\Model\Cate;
		$cateData = Data::tree($cateModel->get(),'cname');
		View::with('cateData',$cateData);
		//2.获取“所属标签”
		$tagModel = new \Common\Model\Tag;
		$tagData = $tagModel->get();
		View::with('tagData',$tagData);
		//3.获得旧数据
		$aid = Q('get.aid',0,'intval');
		$oldData = $this->model->join('article_data','article_aid','=','aid')->where("aid=$aid")->find();
		View::with('oldData',$oldData);
		//4.获得文章所选标签id(所属标签)
		$ArcTagModel = new \Common\Model\ArcTag;
		//lists取得指定字段，并且变为一维数组
		$tids = $ArcTagModel->where("article_aid={$aid}")->lists('tag_tid');
		//如果这篇文章没有标签，那么$tids为null，页面的in_array判断也会报错。
		$tids = $tids ? $tids : array();
		//分配数据
		View::with('tids',$tids);
		//载入模板
	    View::make();
	}
	
	
	
	
	
	
	
	
	
	
}
	